// 业务处理
var PageIndex = {
    init: function () {
        this.bindDrawer()
        this.sideMenu()
        this.activeSideNav()
        this.bindScreenFull()
        this.initHighlighting()
        this.addCodeCopy('.app-main')
        this.bindEvent()
    },
    /**
     * 绑定事件
     */
    bindEvent: function () {
        var self = this,
            demo = $('.demo')

        // 代码显示控制按钮
        demo.on('click', '.code-control', function () {
            var scope = $(this),
                hl = scope.closest('.code-show').find('.hljs-pre')

            if (hl.length) {
                hl.slideToggle()
            }

            return false
        })
    },
    /**
     * 代码高亮
     */
    initHighlighting: function () {
        var self = this,
            _pre = '<pre class="hljs-pre"><code class="hljs lang-html"></code></pre>',
            _show = '' +
                '<div class="code-show">' +
                '    <div class="code-highlight">' +
                '        <textarea class="code-text"></textarea>' +
                '    </div>' +
                '    <pre class="hljs-pre"><code class="hljs"></code></pre>' +
                '    <div class="code-control">' +
                '        <i class="fa fa-code"></i>' +
                '    </div>' +
                '</div>'


        $('.demo-code').each(function (index, item) {
            var scope = $(this),
                source = scope.find('.code-source')

            if (source.length) {
                // $(_show).appendTo(scope).find('.hljs').html(hljs.highlightAuto(source.html()).value)
                $(_show).insertAfter(source).find('.hljs').html(hljs.highlightAuto(source.html()).value)
            }
        })

        /*
        $('.code').each(function(index, block) {
            var scope = $(this),
                code = scope.val(),
                hl = hljs.highlightAuto(code),
                _next = scope.next()

            if (!_next.hasClass('hljs-pre')) {
                $(_pre).insertAfter(scope).children().html(hl.value)
            }

        });
        */
    },
    /**
     * 增加拷贝代码按钮
     * @param selector 选择器
     */
    addCodeCopy: function (selector) {
        var options = {   // optional
            contentSelector: selector,
            // loadDelay:2000,

            // CSS class(es) used to render the copy icon.
            copyIconClass: "fas",
            // CSS class(es) used to render the done icon.
            // checkIconClass: "fa fa-check text-success",
            copyIconContent: " Copy",

            onBeforeTextCopied: function(text, codeElement) {
                return text;   //  you can fix up the text here
            }
        };
        window.highlightJsBadge(options);
    },
    /**
     * 初始化设置
     */
    bindDrawer: function () {
        var settings = {
            tpl: $('#js-app-drawer'),
            dom: {
                container: '#app-drawer',
                close: '.setting-drawer-handle',
                main: '.app-drawer-main'
            }
        }
        $.settingDrawer(settings)
    },
    /**
     * 侧边栏收缩菜单
     */
    sideMenu: function () {
        var self = this

        $('#navSidebar .nav-dropdown').on('click', function(){
            var scope = $(this)
            scope.parent().toggleClass('open').children('.submenu').slideToggle()

            return false
        })
    },
    /**
     * 侧边栏状态选中
     */
    activeSideNav: function () {
        var self = this

        $("#navSidebar .submenu a").each(function (index, val) {
            var scope = $(this)

            if (location.href.indexOf(this.href) !== -1) {
                scope.parents('li').addClass('on')
                scope.parents('.submenu').slideDown()
                self.setBreadTitle(scope.text())
            }
        })
    },
    /**
     * 设置bread名称
     * @param title
     */
    setBreadTitle: function (title) {
        var current = $('.breadcrumb .breadcrumb-item[aria-current="page"]')

        if (current.length && title) {
            current.text(title)
        }
    },
    /**
     * 全屏
     */
    bindScreenFull: function () {
        $('#toggle').click(function () {
            var scope = $(this)
            screenfull.toggle().then(function () {
                console.log('Fullscreen mode: ' + (screenfull.isFullscreen ? 'enabled' : 'disabled'))
                var icon = screenfull.isFullscreen ? 'icon-fullscreen' : 'icon-exit-fullscreen'
                scope.find('svg').toggleClass('fullscreen')
            });
        });
    }
}

$(function(){
    // 页面方法初始化
    PageIndex.init()
});
